package com.etc.threaddemo.deadlock;

/**
 * @author: knowno
 * @create: 2024-09-13 15:56
 * @Version 1.0
 **/
public class TestDeadLock {

    public static Object object1 = new Object();
    public static Object object2 = new Object();

    public static void main(String[] args) {
        //启动线程
        new Thread1().start();
        new Thread2().start();

    }
}

class Thread1 extends Thread {

    @Override
    public void run() {
        //先拿到了object1
        synchronized (TestDeadLock.object1) {
            System.out.println("Thread1中的 lock了 object1");

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            //嵌套一个synchronized
            synchronized (TestDeadLock.object2) {
                System.out.println("Thread1中的 lock了 object2");
            }
        }

    }
}

class Thread2 extends Thread {

    @Override
    public void run() {
        //先拿到了object2
        synchronized (TestDeadLock.object2) {
            System.out.println("Thread2中的 lock了 object2");

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            //嵌套一个synchronized
            synchronized (TestDeadLock.object1) {
                System.out.println("Thread2中的 lock了 object1");
            }
        }

    }
}
